{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用MindSpore实现简单线性函数拟合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 概述\n",
    "\n",
    "回归问题算法通常是利用一系列属性来预测一个值，预测的值是连续的。例如给出一套房子的一些特征数据，如面积、卧室数等等来预测房价，利用最近一周的气温变化和卫星云图来预测未来的气温情况等。如果一套房子实际价格为500万元，通过回归分析的预测值为499万元，则认为这是一个比较好的回归分析。在机器学习问题中，常见的回归分析有线性回归、多项式回归、逻辑回归等。本例子介绍线性回归算法，并通过MindSpore进行线性回归AI训练体验。\n",
    "\n",
    "整体流程如下：\n",
    "\n",
    "1. 生成数据集\n",
    "2. 定义训练网络\n",
    "3. 定义前向传播网络与反向传播网络并关联\n",
    "4. 拟合过程可视化准备\n",
    "5. 执行训练\n",
    "\n",
    "> 本文档适用于CPU、GPU和Ascend环境。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 环境准备\n",
    "\n",
    "设置MindSpore运行配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:52.617310Z",
     "start_time": "2021-01-04T07:04:51.919345Z"
    }
   },
   "outputs": [],
   "source": [
    "from mindspore import context\n",
    "\n",
    "context.set_context(mode=context.GRAPH_MODE, device_target=\"CPU\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`GRAPH_MODE`：图模式。\n",
    "\n",
    "`device_target`：设置MindSpore的训练硬件为CPU。\n",
    "\n",
    "> 本教程代码依赖`matplotlib`第三方支持包，可使用命令`pip install matplotlib`安装。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成数据集\n",
    "\n",
    "### 定义数据集生成函数\n",
    "\n",
    "`get_data`用于生成训练数据集和测试数据集。由于拟合的是线性数据，假定要拟合的目标函数为：$f(x)=2x+3$，那么我们需要的训练数据集应随机分布于函数周边，这里采用了$f(x)=2x+3+noise$的方式生成，其中`noise`为遵循标准正态分布规律的随机数值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:52.623357Z",
     "start_time": "2021-01-04T07:04:52.618320Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def get_data(num, w=2.0, b=3.0):\n",
    "    for _ in range(num):\n",
    "        x = np.random.uniform(-10.0, 10.0)\n",
    "        noise = np.random.normal(0, 1)\n",
    "        y = x * w + b + noise\n",
    "        yield np.array([x]).astype(np.float32), np.array([y]).astype(np.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用`get_data`生成50组测试数据，并可视化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:52.988318Z",
     "start_time": "2021-01-04T07:04:52.624363Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAEICAYAAAC6fYRZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3de3yU5Zn/8c/FwRNYxSVBFBE8tFu1Fdsg1EOrQpWqLZWtVi3Iri7xUFrRdn/i4VdPuxC79dDtzxXSFnRbqIcqeAIFFKwBFYO1cgj+RAUMYBJqBTyFhrn2j+eZMElmkgwzTyYz+b5fr7wyz2HmvnkyXLlzP9dct7k7IiJSmLrlugMiIhIdBXkRkQKmIC8iUsAU5EVECpiCvIhIAVOQFxEpYAryIs2Y2RIz+9d2nnuamVVH3SeRPaUgL3nLzNab2adm9lHC1//Ldb9SMbN/NrOKXPdDupYeue6ASIa+7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xUdIOVf07m4XgkS30epYtGzwJlm1iecWj0z3JeeqO8sZ/OLIDBVA/VADfBswrEbCTIj3gS+lbB/HnBI+PgIguC/DngE2DvCvt4PXNFs3yHAvIS+/CX8Wk0wbRH19fsdsBJ4I3yT9W/er3D7bILsjbc7qF/rCOYeXw+/pjXvV0ddr2T/duA2gl9AAPuE75114XvpiKivT9juKQR/qr+RcJ3OBq6Iv8+AieG1+QvBDeyTOqBfSX8uzfplwL3hNV1JQmZcxH3bjyBoH5Cwr8OvF8EvmS3A38P4dRnBfZzngLfC7weF55YAv0l47qXhe20d8C970r7KGoiIFLBCma4REZEkFORFRAqYgryISAFTkBcRKWAK8iIiBUxBXkSkgCnIi4gUsP8FUy7XCPXim48AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "eval_data = list(get_data(50))\n",
    "x_target_label = np.array([-10, 10, 0.1])\n",
    "y_target_label = x_target_label * 2 + 3\n",
    "x_eval_label,y_eval_label = zip(*eval_data)\n",
    "\n",
    "plt.scatter(x_eval_label, y_eval_label, color=\"red\", s=5)\n",
    "plt.plot(x_target_label, y_target_label, color=\"green\")\n",
    "plt.title(\"Eval data\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上图中绿色线条部分为目标函数，红点部分为验证数据`eval_data`。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义数据增强函数\n",
    "\n",
    "使用MindSpore的数据增强函数，将数据进行增强操作，操作解释如下：\n",
    "\n",
    "- `ds.GeneratorDataset`：将生成的数据转换为MindSpore的数据集，并且将生成的数据的x，y值存入到`data`和`label`的数组中。\n",
    "- `batch`：将`batch_size`个数据组合成一个batch。\n",
    "- `repeat`：将数据集数量倍增。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:52.993381Z",
     "start_time": "2021-01-04T07:04:52.990360Z"
    }
   },
   "outputs": [],
   "source": [
    "from mindspore import dataset as ds\n",
    "\n",
    "def create_dataset(num_data, batch_size=16, repeat_size=1):\n",
    "    input_data = ds.GeneratorDataset(list(get_data(num_data)), column_names=['data','label'])\n",
    "    input_data = input_data.batch(batch_size)\n",
    "    input_data = input_data.repeat(repeat_size)\n",
    "    return input_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用数据集增强函数生成训练数据，并查看训练数据的格式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.079377Z",
     "start_time": "2021-01-04T07:04:52.994402Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The dataset size of ds_train: 100\n",
      "dict_keys(['data', 'label'])\n",
      "The x label value shape: (16, 1)\n",
      "The y label value shape: (16, 1)\n"
     ]
    }
   ],
   "source": [
    "data_number = 1600\n",
    "batch_number = 16\n",
    "repeat_number = 1\n",
    "\n",
    "ds_train = create_dataset(data_number, batch_size=batch_number, repeat_size=repeat_number) \n",
    "print(\"The dataset size of ds_train:\", ds_train.get_dataset_size())\n",
    "dict_datasets = next(ds_train.create_dict_iterator())\n",
    "\n",
    "print(dict_datasets.keys())\n",
    "print(\"The x label value shape:\", dict_datasets[\"data\"].shape)\n",
    "print(\"The y label value shape:\", dict_datasets[\"label\"].shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过定义的`create_dataset`将生成的1600个数据增强为了100组shape为16x1的数据集。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义训练网络\n",
    "\n",
    "在MindSpore中使用`nn.Dense`生成单个数据输入，单个数据输出的线性函数模型：\n",
    "\n",
    "$$f(x)=wx+b\\tag{1}$$\n",
    "\n",
    "并使用Normal算子随机初始化权重$w$和$b$。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.085026Z",
     "start_time": "2021-01-04T07:04:53.080390Z"
    }
   },
   "outputs": [],
   "source": [
    "from mindspore.common.initializer import Normal\n",
    "from mindspore import nn\n",
    "\n",
    "class LinearNet(nn.Cell):\n",
    "    def __init__(self):\n",
    "        super(LinearNet, self).__init__()\n",
    "        self.fc = nn.Dense(1, 1, Normal(0.02), Normal(0.02))\n",
    "        \n",
    "    def construct(self, x):\n",
    "        x = self.fc(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "调用网络查看初始化的模型参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.100773Z",
     "start_time": "2021-01-04T07:04:53.086027Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter (name=fc.weight) [[-0.02289871]]\n",
      "Parameter (name=fc.bias) [0.01492652]\n"
     ]
    }
   ],
   "source": [
    "net = LinearNet()\n",
    "model_params = net.trainable_params()\n",
    "for param in model_params:\n",
    "    print(param, param.asnumpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "初始化网络模型后，接下来将初始化的网络函数和训练数据集进行可视化，了解拟合前的模型函数情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.242097Z",
     "start_time": "2021-01-04T07:04:53.102786Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD8CAYAAAB3u9PLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh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YfAzf4ehu1BXblVIdEQYE1ALFwPfPI+x4s6DPwM+BCM6vmqzq+0LfuyKLQat4db/baD2QCn+XZNgRvL49fgIvzrJ8GnOB7fRLeRLEKeB8oH6G4xwN98607AZiWJ67vfT/L8FaNRPravgn8CPzge9Mk54/Tt9wNb8+U1VGKcxXe+s0lvp/R+eOM1vUMdG2Ah/AmL4AKvvfdKt/78KQoXL/2eG//f8hzDbsBfXPeo0A/33X7Hm/DfbsoxBnwb5gvTgNe8F3vH8nT8zDCsVbE+wF/XJ51Ub+eeBPUJuCg73OzN942q5nASt/v4337pgCv5Dn2Jt/7dBVwY1HK09AVIiICxFaVkYiIRJESgoiIAEoIIiLio4QgIiKAEoKIiPgoIYiICKCEICIiPv8PsVW4qWEViusAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mindspore import Tensor\n",
    "\n",
    "x_model_label = np.array([-10, 10, 0.1])\n",
    "y_model_label = (x_model_label * Tensor(model_params[0]).asnumpy()[0][0] + \n",
    "                 Tensor(model_params[1]).asnumpy()[0])\n",
    "\n",
    "plt.axis([-10, 10, -20, 25])\n",
    "plt.scatter(x_eval_label, y_eval_label, color=\"red\", s=5)\n",
    "plt.plot(x_model_label, y_model_label, color=\"blue\")\n",
    "plt.plot(x_target_label, y_target_label, color=\"green\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上图中可以看出，蓝色线条的初始化模型函数与绿色线条的目标函数还是有较大的差别的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义前向传播网络与反向传播网络并关联"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下来需要定义模型的损失函数，这里采用均方误差（MSE，Mean Squared Error）的方法用于判断拟合的效果如何，即均方误差值越小，拟合的效果越好，其损失损失函数公式为：\n",
    "\n",
    "$$J(w)=\\frac{1}{2m}\\sum_{i=1}^m(h(x_i)-y^{(i)})^2\\tag{2}$$\n",
    "\n",
    "假设训练数据第$i$个数据为$(x_i,y^{(i)})$，公式2中的参数解释如下：\n",
    "\n",
    "- $J(w)$为损失值。\n",
    "\n",
    "- $m$为样本数据的数量，本例中$m$的值为`batch_number`。\n",
    "\n",
    "- $h(x_i)$为第$i$个数据的$x_i$值代入模型网络（公式1）后的预测值。\n",
    "\n",
    "- $y^{(i)}$为第$i$个数据中的$y^{(i)}$值（label值）。\n",
    "\n",
    "### 定义前向传播网络\n",
    "\n",
    "前向传播网络包含两个部分，其中：\n",
    "\n",
    "1. 将参数带入到模型网络中得出预测值。\n",
    "2. 使用预测值和训练数据计算出loss值。\n",
    "\n",
    "在MindSpore中使用如下方式实现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.249228Z",
     "start_time": "2021-01-04T07:04:53.243109Z"
    }
   },
   "outputs": [],
   "source": [
    "net = LinearNet()\n",
    "net_loss = nn.loss.MSELoss()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义反向传播网络\n",
    "\n",
    "反向传播网络的目标是不断变换权重值，使得loss值取得最小值，一般的在线性网络中采用权重更新公式：\n",
    "\n",
    "$$w_{t}=w_{t-1}-\\alpha\\frac{\\partial{J(w_{t-1})}}{\\partial{w}}\\tag{3}$$\n",
    "\n",
    "公式3参数解释：\n",
    "\n",
    "- $w_{t}$为迭代后的权重值。\n",
    "- $w_{t-1}$为迭代前的权重值。\n",
    "- $\\alpha$为学习率。\n",
    "- $\\frac{\\partial{J(w_{t-1}\\ )}}{\\partial{w}}$为损失函数对权重$w_{t-1}$的微分。\n",
    "\n",
    "函数中所有的权重值更新完成后，将值传入到模型函数中，这个过程就是反向传播过程，实现此过程需要使用MindSpore中的优化器函数，如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.273562Z",
     "start_time": "2021-01-04T07:04:53.250245Z"
    }
   },
   "outputs": [],
   "source": [
    "opt = nn.Momentum(net.trainable_params(), learning_rate=0.005, momentum=0.9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关联前向和反向传播网络\n",
    "\n",
    "定义完成前向传播和反向传播后，在MindSpore中需要调用`Model`函数，将前面定义的网络，损失函数，优化器函数关联起来，使之变成完整的计算网络。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.287238Z",
     "start_time": "2021-01-04T07:04:53.275579Z"
    }
   },
   "outputs": [],
   "source": [
    "from mindspore import Model\n",
    "\n",
    "model = Model(net, net_loss, opt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 拟合过程可视化准备\n",
    "\n",
    "### 定义绘图函数\n",
    "\n",
    "为了使得整个训练过程更容易理解，需要将训练过程的测试数据、目标函数和模型网络进行可视化，这里定义了可视化函数，将在每个step训练结束后调用，展示模型网络的拟合过程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.305631Z",
     "start_time": "2021-01-04T07:04:53.288251Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import time\n",
    "\n",
    "def plot_model_and_datasets(net, eval_data):\n",
    "    weight = net.trainable_params()[0]\n",
    "    bias = net.trainable_params()[1]\n",
    "    x = np.arange(-10, 10, 0.1)\n",
    "    y = x * Tensor(weight).asnumpy()[0][0] + Tensor(bias).asnumpy()[0]\n",
    "    x1, y1 = zip(*eval_data)\n",
    "    x_target = x\n",
    "    y_target = x_target * 2 + 3\n",
    "    \n",
    "    plt.axis([-11, 11, -20, 25])\n",
    "    plt.scatter(x1, y1, color=\"red\", s=5)\n",
    "    plt.plot(x, y, color=\"blue\")\n",
    "    plt.plot(x_target, y_target, color=\"green\")\n",
    "    plt.show()\n",
    "    time.sleep(0.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义回调函数\n",
    "\n",
    "MindSpore提供的工具，可对模型训练过程进行自定义控制，这里在`step_end`中调用可视化函数，展示拟合过程。更多的使用可参考[官网说明](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#callback)\n",
    "\n",
    "- `display.clear_output`：清除打印内容，实现动态拟合效果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:04:53.318392Z",
     "start_time": "2021-01-04T07:04:53.306647Z"
    }
   },
   "outputs": [],
   "source": [
    "from IPython import display\n",
    "from mindspore.train.callback import Callback\n",
    "\n",
    "class ImageShowCallback(Callback):\n",
    "    def __init__(self, net, eval_data):\n",
    "        self.net = net\n",
    "        self.eval_data = eval_data\n",
    "        \n",
    "    def step_end(self, run_context):\n",
    "        plot_model_and_datasets(self.net, self.eval_data)\n",
    "        display.clear_output(wait=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 执行训练\n",
    "\n",
    "完成以上过程后，可以使用训练数`ds_train`对模型训练，这里调用`model.train`进行，其中参数解释：\n",
    "\n",
    "- `epoch`：训练迭代的整个数据集的次数。\n",
    "- `ds_train`：训练数据集。\n",
    "- `callbacks`：训练过程中需要调用的回调函数。\n",
    "- `dataset_sink_model`：数据集下沉模式，支持Ascend、GPU计算平台，本例为CPU计算平台设置为False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-04T07:05:27.693120Z",
     "start_time": "2021-01-04T07:04:53.319412Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAD8CAYAAACSCdTi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Vx8i+j7Afr2xy87Mf2SYp8Ds+maM/invCzErgT/BjnHMTs6/PmvSdc9PNbLCZVXHOeVqMKxe/l0iWObkeWOycS86+IlLHKyDZzGo457YEuq62BdkmCf91gyNq4b8emSfR0l1z3DIKgcTxDfCfwKJ7gZw+GYRDC+AX51xSsJVmVtbMyh95jP/i44pg24ZLtn7Qm3NobyFwlvlHIpXE/1F3ssdx5VQeI+s2+XG8cvOzT8b/3gH/e+nrnP4phVOg338EkOicezOHbU49cn3AzJrh//ve7nFcufm9TAbuCYyyuQTYfaSrIh/k+Gk6Escri6zvo5xy0VfAtWZWKdC1em1gWd54fWU5nF/4E1MSkAYkA19lWdcd/8iI1cD1WZZPB2oGHtfHn/zXAp8ApTyMdSTQKduymsD0LLEsDXytxN9t4fXx+xBYDiwLvMlqZI8r8LwV/tEb6/IprrX4+x6XBL6GZo8rv45XsJ8d6IP/HxBA6cB7Z23gvVTf6+MTaPfv+D+qL8tynFoBnY68z4CHA8dmKf4L2M3zIa6gv5dscRkwKHBMl5NlZJzHsZ2EP2mfnGVZvh8v/P9ktgCHA/nrAfzXceYAawLfKwe2jQXey/LadoH32lrg/hNpX2UNRESiWLR014iISBBK8iIiUUxJXkQkiinJi4hEMSV5EZEopiQvIhLFlORFRKLY/wNQ0IMDOgZ73wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter (name=fc.weight) [[2.0064354]]\n",
      "Parameter (name=fc.bias) [2.9529438]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from mindspore.train.callback import LossMonitor\n",
    "\n",
    "epoch = 1\n",
    "imageshow_cb = ImageShowCallback(net, eval_data)\n",
    "model.train(epoch, ds_train, callbacks=[imageshow_cb], dataset_sink_mode=False)\n",
    "\n",
    "plot_model_and_datasets(net, eval_data)\n",
    "for net_param in net.trainable_params():\n",
    "    print(net_param, net_param.asnumpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-14T04:00:18.787349Z",
     "start_time": "2020-09-14T04:00:18.784236Z"
    }
   },
   "source": [
    "训练完成后打印出最终模型的权重参数，其中weight接近于2.0，bias接近于3.0，模型训练完成，符合预期。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 总结\n",
    "\n",
    "本次体验我们了解了线性拟合的算法原理，并在MindSpore框架下实现了相应的算法定义，了解了线性拟合这类的线性回归模型在MindSpore中的训练过程，并最终拟合出了一条接近目标函数的模型函数。另外有兴趣的可以调整数据集的生成区间从（-10,10）扩展到（-100,100），看看权重值是否更接近目标函数；调整学习率大小，看看拟合的效率是否有变化；当然也可以探索如何使用MindSpore拟合$f(x)=ax^2+bx+c$这类的二次函数或者更高次的函数。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
